Click here to Skip to main content
15,887,585 members
Articles / Programming Languages / C#

Help Button Emulation

Rate me:
Please Sign up or sign in to vote.
4.67/5 (10 votes)
25 Mar 2009CPOL1 min read 50.2K   1.2K   28   7
Help button emulation using Win32 API function

Introduction

Some time ago, while I was writing an application, I required a Help Button. The functionality of this button is widely known: by clicking on this first and then on any control of the client area, you will receive a tooltip including help and information about the element itself.
Adding a Help Button in a C# program is relatively simple. 

For this purpose, there is a property named Form.HelpButton, which when true, makes a small button with a question mark appear in the caption bar to the left of the Close button. At this point, however, a problem arises, the property is ignored if one (or both), of these two properties, Form.MinimizeBox and Form.MaximizeBox are true. In other words, to have a Help Button, we should renounce to Window's minimization and maximization.
So, is there a way to avoid this? The answer is yes, by using help button emulation!

The Code

To resolve this task we will use a function, from the Win32 API called SendMessage. This function, which sends a message to a window, called with some specific parameters is all we need. That said, the first thing to do is to import the DLL which contains SendMessage and declare two constants we need:

C#
[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )]
private static extern IntPtr SendMessage
	( IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam );

private const int WM_SYSCOMMAND = 0x112;
private const int SC_CONTEXTHELP = 0xf180;

Once this is done, we must use a HelpProvider object whose job is to intercept the help request and show the appropriate tooltip:

C#
private System.Windows.Forms.HelpProvider helpProvider1;

// Set the Help string associated with the textBox1.
helpProvider1.SetHelpString( textBox1, "Sample help string." );
helpProvider1.SetShowHelp( textBox1, true );

Finally, we deal with the real help button's emulation. In the Click event, we must call the SendMessage function with appropriate parameters:

C#
private void btnHelp_Click( object sender, EventArgs e )
{
 // Setting Capture to false, will make the control 
 // not receive all mouse events: the window procedure doesn't receive
 // messages when the mouse is outside the window's client area.
 btnHelp.Capture = false;
 // Simulate the help button press with SendMessage function.
 SendMessage( this.Handle, WM_SYSCOMMAND, ( IntPtr )SC_CONTEXTHELP, IntPtr.Zero );
}

That's all! In these simple steps, we have a window containing both a Help Button and minimize and maximize.

History

  • 25th March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionhelpProvider in window form Pin
ttson2413-Mar-17 4:19
ttson2413-Mar-17 4:19 
QuestionAny way to make it work for a ToolStripButton? Pin
Nev16-Sep-09 21:14
Nev16-Sep-09 21:14 
AnswerRe: Any way to make it work for a ToolStripButton? Pin
Salvatore Tipaldi17-Sep-09 1:42
Salvatore Tipaldi17-Sep-09 1:42 
GeneralRe: Any way to make it work for a ToolStripButton? Pin
Nev17-Sep-09 13:10
Nev17-Sep-09 13:10 
GeneralRe: Any way to make it work for a ToolStripButton? Pin
Member 372698110-Feb-12 22:36
Member 372698110-Feb-12 22:36 
GeneralThanks :) Pin
spif200130-Mar-09 21:13
spif200130-Mar-09 21:13 
GeneralRe: Thanks :) Pin
Salvatore Tipaldi31-Mar-09 2:43
Salvatore Tipaldi31-Mar-09 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.